<?php

namespace system\components;

/**
 * {@link #DocumentComponent} element responsable for
 * agregating debugging and profiling information
 *
 * XML representation:
 * 	 <code>
 * 	 		<DebugComponent>
 * 	 			<time></time>
 *                              <xmlDump></xmlDump>
 * 	 		</DebugComponent>
 * 	 </code>
 *
 * @see DocumentComponent
 * @package system\components
 * @author Pavel Egorov {@link mailto:pavel@egorov.ru pavel@egorov.ru}
 * @version 0.0.1 $Revision: -1 $
 * @copyright Copyright (c) 2011, Pavel Egorov
 */
class DebugComponent extends DocumentComponent {

    private $startTime;
    public $time;
    public $xmlDump;
    public $globals;

    /**
     * Creates an instance of {@link DebugComponent} and saves current time
     */
    public function __construct() {
        parent::__construct("DebugComponent");
        $this->startTime = microtime(true);
    }

    /**
     * stop() - measures time from startTime
     */
    public function stop() {
        $this->time = \round(microtime(true) - $this->startTime,5);
    }

    /**
     * Sets XML dump
     * @param DOMDocument $dom DOMDocument
     */
    public function setXMLDump(\DOMDocument $dom){
        $this->xmlDump = $dom->saveXML();
    }

    /**
     * Adds superglobal variable with given name into array of superglobals
     * @param String $globalArrayName
     * @param Array $globalArray
     */
    public function setGlobalArray($globalArrayName,$globalArray){
        $superglobal['superglobalvariable']['superglobalname'] = $globalArrayName;
        $superglobal['superglobalvariable']['superglobalvariables'] =  ($globalArray == null)? null : $this->enrollArray($globalArray);
        $this->globals[] = $superglobal;
    }

    /**
     *
     * @access private
     */
    private function enrollArray($array){
        $result = null;
        foreach ($array as $key => $value) {
            $result[]['variable'] = array('name'=>$key,'value'=>$value);
        }
        return $result;
    }
}